package com.nidu.demo.notify.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.notify.convertor.NotifyTemplateConvertor;
import com.nidu.demo.notify.dto.NotifyTemplateCO;
import com.nidu.demo.notify.gateway.NotifyTemplateGateway;
import com.nidu.demo.notify.model.NotifyTemplate;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 站内信消息模板单个查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class NotifyTemplateSingleQryExe {

    private final NotifyTemplateGateway gateway;
    private final NotifyTemplateConvertor convertor;

    public SingleResponse<NotifyTemplateCO> execute(Long id) {
        // 1. 根据ID查询站内信消息
        NotifyTemplate notifyTemplate = gateway.getById(id);
        if (notifyTemplate == null) {
            throw new BizException(ErrorCodeConstants.NOTIFY_TEMPLATE_NOT_FOUND.getCode(), ErrorCodeConstants.NOTIFY_TEMPLATE_NOT_FOUND.getMessage());
        }

        // 2. 转换为DTO返回
        NotifyTemplateCO notifyTemplateCO = convertor.toClientObject(notifyTemplate);
        return SingleResponse.of(notifyTemplateCO);
    }

}
